<template>
    <div class="home">
        <h1>前端演示SignalR</h1>
        <input v-model="user" type="text"/>
        <input v-model="message" type="text"/>
        <button @click="sendOwn">对自己发送</button>

        <div>
            <ul v-for="(item ,index) in messages" v-bind:key="index +'itemMessage'">
                <li>{{item.user}} says {{item.message}}</li>
            </ul>
        </div>
    </div>
</template>

<script>
    // @ is an alias to /src
    import * as signalR from "@microsoft/signalr";
    import {getAccseeToken} from "../api/manage"

    export default {
        name: "Home",
        components: {},
        data() {
            return {
                user: "371312199403015117", //用户
                message: {
                    To2: "",
                    Content: {
                        Sfzh:"371312199403015117",
                        NickName2:"郁万龙",
                        PhoneNum:"",
                        
                    },
                    TransferCode: 0,
                    LocalServerCode: 0,
                    LocalServerTime: new Date(),
                    From: ""
                }, //消息
                connection: "", //signalr连接
                messages: [{user: "371312199403015117", message: "测试"}],
                token: ''//返回消息
            };
        },
        methods: {

            //只给自己发送消息
            sendOwn: function () {
                //this.message.Content=JSON.stringify(this.message.Content)
                this.connection
                    .invoke("SendFaceInfoToUser", this.message, this.user)
                    .catch(function (err) {
                        return console.error(err);
                    });
            },
            //getAccessToken
            getToken() {
                let that = this;
                let param = {
                    appID: "doctor2703",
                    appSecret: "jkgly@163.com@2703",
                    appUserName: "yuwanlong",
                    appUserPassword: "ywl123456",
                    appUserSFZH: "371312199403015117"

                }
                let url = '/api/authentication/GetToken'
                getAccseeToken(url, param).then(res => {
                    console.log(res)
                    that.token = res.access_token
                    this.linkSignalr();
                })
            },
            //系统发送消息

            linkSignalr() {
                this.connection = new signalR.HubConnectionBuilder()
                    .withUrl("/chatHub2", {
                        accessTokenFactory: () => this.token
                        //skipNegotiation: true,
                        , transport: signalR.HttpTransportType.LongPolling
                    })
                    .withAutomaticReconnect()
                    .build();
                this.connection.on("ReceiveFaceInfo", function (data) {
                    console.log(data)
                });
                this.connection.start();
            }
        },
        created: function () {
            this.getToken();
        }
    };
</script>